/*
 * Copyright @ 2025 weili23
 * com.example.ai 10:37
 * All right reserved.
 */

package com.example.ai;

import com.example.ai.domain.po.Person;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;

/**
 * @desc:批量新增、删除 文档
 * @author: weili23
 * @createTime: 2025/8/14 10:37
 * @version: v1.0
 */
@SpringBootTest
public class BulkDocument {
    public static final String INDEX_NAME = "person";
    @Resource
    private RestHighLevelClient client;

    //批量新增
    @Test
    void bulkAddDocument() throws IOException {
        //准备新增数据
        Person perso1 = new Person("10", "张飞", 18, new Date(), "张飞，字翼德，涿郡人，蜀汉名将 。他与关羽同称 “万人敌”。黄巾起义时，随刘备平乱，开启征战生涯。长坂坡仅率二十余骑断后，喝退曹军。入川时义释严颜，巴西之战大败张郃。但他性暴，对部下严苛，在伐吴前夕，被部将范强、张达刺杀，谥桓侯 。");
        Person perso2 = new Person("11", "刘备", 19, new Date(), "刘备（161 年 - 223 年），字玄德，涿郡涿县人，蜀汉开国皇帝。他乃西汉中山靖王刘胜之后，早年家贫，以织席贩履为生 。刘备胸怀大志，善交豪杰，凭镇压黄巾起义崭露头角。此后历经波折，先得徐州又失去，后于赤壁之战联吴抗曹，占荆州，取益州，称汉中王，终在成都称帝。");
        Person perso3 =new Person("12", "关羽", 20, new Date(), "关羽，字云长，河东郡解县人 。因早年犯事，流落涿郡，与刘备、张飞桃园结义。他武艺绝伦，为 “万人敌”。曾暂降曹操，斩颜良立功，而后挂印封金寻刘备。赤壁之战后镇守荆州，建安二十四年北伐，水淹七军，威震华夏。后遭东吴偷袭，败走麦城被杀 。其忠义精神为后世敬仰，尊为 “武圣” 。");
        Person perso4 =new Person("13", "曹操", 21, new Date(), "曹操（155 年 - 220 年），字孟德，沛国谯县人，三国曹魏奠基者。他兼具雄才与争议，破黄巾、讨董卓，挟天子以令诸侯。灭袁绍、征乌桓，统一北方。推行屯田，兴修水利，重视人才。善诗文，开建安文学之风。晚年封魏王，其子曹丕代汉后追尊为武帝，是政治家、军事家与文学家。");
        //转json
        ObjectMapper objectMapper = new ObjectMapper();
        String json1 = objectMapper.writeValueAsString(perso1);
        String json2 = objectMapper.writeValueAsString(perso2);
        String json3 = objectMapper.writeValueAsString(perso3);
        String json4 = objectMapper.writeValueAsString(perso4);
        IndexRequest request1 = new IndexRequest(INDEX_NAME).id(perso1.getId()).source(json1, XContentType.JSON);
        IndexRequest request2 = new IndexRequest(INDEX_NAME).id(perso2.getId()).source(json2, XContentType.JSON);
        IndexRequest request3 = new IndexRequest(INDEX_NAME).id(perso3.getId()).source(json3, XContentType.JSON);
        IndexRequest request4 = new IndexRequest(INDEX_NAME).id(perso4.getId()).source(json4, XContentType.JSON);
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.add(request1);
        bulkRequest.add(request2);
        bulkRequest.add(request3);
        bulkRequest.add(request4);
        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(bulkResponse);

    }


    //批量删除
    @Test
    void deleteBulk() throws IOException {
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.add(new DeleteRequest(INDEX_NAME, "10"));
        bulkRequest.add(new DeleteRequest(INDEX_NAME, "11"));
        bulkRequest.add(new DeleteRequest(INDEX_NAME, "12"));
        bulkRequest.add(new DeleteRequest(INDEX_NAME, "13"));
        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(bulkResponse);
    }
}
